package com.nuctech.hardwaredriver.serialport_library.core.codec;


import com.nuctech.hardwaredriver.util.DefaultConfigUtil;

import java.io.IOException;
import java.io.InputStream;

/**
 * Created by 心魂free on 2019/11/1.
 * <p>
 * 默认解码器
 */

public class DefaultDecoder implements SerialPortProtocolDecoder{

    @Override
    public byte[] decoder(InputStream in) throws IOException{

        int totalLength = DefaultConfigUtil.receiveDataTotalLength;
        if (totalLength <= 0) {

            return null;
        }

        byte[] receiveDataBytes = new byte[totalLength];
        int offset;
        int sum = 0;

        while (sum != totalLength) {

            offset = in.read(receiveDataBytes, sum, totalLength - sum);
            sum += offset;
        }

        return receiveDataBytes;
    }
}
